﻿using System;
using SteroMood;

namespace SteroClood.MediaPlayers
{
    class FmodMediaPlayer : IMediaPlayer
    {
        private EndTackEventHandler _onEndMusicEvent;
        public bool Initialize()
        {
            FmodHelper.Initialize();
            FmodHelper.EndMusic += new EndMusicEventHandler(FmodHelper_EndMusic);
            return true;
        }

        void FmodHelper_EndMusic(object sender, EventArgs e)
        {
            if (_onEndMusicEvent != null)
            {
                _onEndMusicEvent();
            }
        }

        public void Shutdown()
        {
            throw new NotImplementedException();
        }

        public bool LoadSong(string pathOrUrl)
        {
            return FmodHelper.Load(pathOrUrl);
        }

        public bool PlayPause()
        {
            return FmodHelper.Pause();
        }

        public EndTackEventHandler OnEndMusicEvent
        {
            get
            {
                return _onEndMusicEvent;
            }
            set
            {
                _onEndMusicEvent += value;
            }
        }


        public void SetVolume(int volume)
        {
            FmodHelper.SetVolume(volume);
        }

        public MediaPlayerStatus GetStatus()
        {
            FmodHelper.FmodStatus status = FmodHelper.GetStatus();
            if (status == null)
                return null;

            MediaPlayerStatus mediaPlayerStatus = new MediaPlayerStatus { IsPaused = status.IsPaused, IsPlaying = status.IsPlaying, SongLengthInMs = status.LengthMs, SongPositionInMs = status.PositionMs };
            if (mediaPlayerStatus.IsPlaying)
            {
                string pos = string.Format("{0:00}:{1:00}", mediaPlayerStatus.SongPositionInMs / (60 * 1000), (mediaPlayerStatus.SongPositionInMs / 1000) % 60);
                string length = string.Format("{0:00}:{1:00}", mediaPlayerStatus.SongLengthInMs / (60 * 1000), (mediaPlayerStatus.SongLengthInMs / 1000) % 60);
                mediaPlayerStatus.Status = (mediaPlayerStatus.IsPaused ? "Paused" : "Playing") + " " + pos + " / " + length;
            }
            else
            {
                mediaPlayerStatus.Status = "Stopped";
            }

            return mediaPlayerStatus;
        }
    }
}
